<!DOCTYPE html>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<div contenteditable id="div"></div>
<button id="button">bold</button>
<script>
const div = document.getElementById('div');
const button = document.getElementById('button');
button.onclick = () => document.execCommand('bold');

const divX = div.offsetLeft + div.offsetWidth / 2;
const divY = div.offsetTop + div.offsetHeight / 2;

const buttonX = button.offsetLeft + button.offsetWidth / 2;
const buttonY = button.offsetTop + button.offsetHeight / 2;

// Regression test for crbug.com/785147
// This test requires setting selection by mouse to test code in SelectionController.
promise_test(() => {
  return new Promise((resolve, reject) => {
    assert_not_equals(window.chrome, undefined);
    assert_not_equals(window.chrome.gpuBenchmarking, undefined);

    chrome.gpuBenchmarking.pointerActionSequence([{
      source: 'mouse',
      actions: [
        // Mouse click to cursor in DIV
        {name: 'pointerDown', x: divX, y: divY},
        {name: 'pointerUp'},
        // Click the button to set bold editing style and also move focus away from DIV
        {name: 'pointerDown', x: buttonX, y: buttonY},
        {name: 'pointerUp'},
        // Click in DIV to set the cursor back without changing selection
        {name: 'pointerDown', x: divX, y: divY},
        {name: 'pointerUp'}
      ]
    }], resolve);
  }).then(() => {
    document.execCommand('insertText', false, 'bar');
    assert_equals(div.innerHTML, '<b>bar</b>');
  })
}, 'Mouse click without selection change should not clear editing style');
</script>
